package com.yanqu.road.server.cmd.activity.seacraft.pirateriot;

import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotUserData;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotConfig;
import com.yanqu.road.pb.activity.SeacraftPirateRiotProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.seacraft.SeacraftPirateRiotModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftPirateRiotMgr;
import com.yanqu.road.server.pb.SeacraftPirateRiotPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SEACRAFT_PIRATE_RIOT_GET_USER_DATA, desc = "请求玩家数据")
public class SeacraftPirateRiotGetUserDataCmd extends AbstractCommand {

    private SeacraftPirateRiotUserData userData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftPirateRiotConfig config = SeacraftPirateRiotMgr.getPirateRiotConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_SEACRAFT_PIRATE_RIOT_NO_IN_TIME;
            return;
        }
        userData = player.getModule(SeacraftPirateRiotModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SEACRAFT_PIRATE_RIOT_NO_UNLOCK;
            return;
        }

        // 刷新下战力
        player.getModule(SeacraftPirateRiotModule.class).refreshBattlePatrons(config, userData);
    }

    @Override
    public void sendData(GamePlayer player) {
        SeacraftPirateRiotProto.SeacraftPirateRiotGetUserDataRespMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotGetUserDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (userData != null) {
            builder.setUserData(SeacraftPirateRiotPb.parseSeacraftPirateRiotUserDataMsg(player, userData));
        }
        player.sendPacket(ClientProtocol.U_SEACRAFT_PIRATE_RIOT_GET_USER_DATA, builder);
    }
}
